package cn.edu.heuet.bnve.model;

import static android.content.Context.ALARM_SERVICE;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import java.io.Serializable;
import java.util.Calendar;

import cn.edu.heuet.bnve.activity.TableImportActivity;
import cn.edu.heuet.bnve.remind.AlarmAlertBroadcastReceiver;
import cn.edu.heuet.bnve.utils.SharepreferencesUtilSystemSettings;

public class Schedule implements Serializable {

    private int id;
    private String title;
    private int isAllday;//是否是全天
    private int isVibrate;//是否震动
    private int year;
    private int month;
    private int day;
    private int startTimeHour;
    private int startTimeMinute;
    private int endTimeHour;
    private int endTimeMinute;
    private int alarmTimeType;
    private int alarmColor;
    private String local;
    private String description;
    private int type;
    private int isComplete;


    public Schedule() {

        alarmTimeType = -1;
        alarmColor = 0;
        local = "无";
        isComplete = 0;
        description = "无";
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getIsAllday() {
        return isAllday;
    }

    public void setIsAllday(int isAllday) {
        this.isAllday = isAllday;
    }

    public int getIsVibrate() {
        return isVibrate;
    }

    public void setIsVibrate(int isVibrate) {
        this.isVibrate = isVibrate;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getStartTimeHour() {
        return startTimeHour;
    }

    public void setStartTimeHour(int startTimeHour) {
        this.startTimeHour = startTimeHour;
    }

    public int getStartTimeMinute() {
        return startTimeMinute;
    }

    public void setStartTimeMinute(int startTimeMinute) {
        this.startTimeMinute = startTimeMinute;
    }

    public int getEndTimeHour() {
        return endTimeHour;
    }

    public void setEndTimeHour(int endTimeHour) {
        this.endTimeHour = endTimeHour;
    }

    public int getEndTimeMinute() {
        return endTimeMinute;
    }

    public void setEndTimeMinute(int endTimeMinute) {
        this.endTimeMinute = endTimeMinute;
    }

    public int getAlarmTimeType() {
        return alarmTimeType;
    }

    public void setAlarmTimeType(int alarmTimeType) {
        this.alarmTimeType = alarmTimeType;
    }

    public int getAlarmColor() {
        return alarmColor;
    }

    public void setAlarmColor(int alarmColor) {
        this.alarmColor = alarmColor;
    }


    public String getLocal() {
        return local;
    }

    public void setLocal(String local) {
        this.local = local;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getIsComplete() {
        return isComplete;
    }

    public void setIsComplete(int isComplete) {
        this.isComplete = isComplete;
    }

    @Override
    public String toString() {
        return "Schedule{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", isAllday=" + isAllday +
                ", isVibrate=" + isVibrate +
                ", year=" + year +
                ", month=" + month +
                ", day=" + day +
                ", startTimeHour=" + startTimeHour +
                ", startTimeMinute=" + startTimeMinute +
                ", endTimeHour=" + endTimeHour +
                ", endTimeMinute=" + endTimeMinute +
                ", alarmTime='" + alarmTimeType + '\'' +
                ", alarmColor='" + alarmColor + '\'' +
                ", local='" + local + '\'' +
                ", description='" + description + '\'' +
                ", isComplete='" + isComplete + '\'' +
                '}';
    }

    public Schedule(String title, int startTimeHour, int startTimeMinute, int endTimeHour, int endTimeMinute, String local, String description,int alarmColor) {
        this.title = title;
        this.startTimeHour = startTimeHour;
        this.startTimeMinute = startTimeMinute;
        this.endTimeHour = endTimeHour;
        this.endTimeMinute = endTimeMinute;
        this.local = local;
        this.description = description;
        this.alarmColor = alarmColor;
    }

    public Schedule(String title, int isAllday, int isVibrate, int year, int month, int day, int startTimeHour,
                    int startTimeMinute, int endTimeHour, int endTimeMinute, int alarmTimeType, int alarmColor,
                    String local, String description, int type, int isComplete) {
        this.title = title;
        this.isAllday = isAllday;
        this.isVibrate = isVibrate;
        this.year = year;
        this.month = month;
        this.day = day;
        this.startTimeHour = startTimeHour;
        this.startTimeMinute = startTimeMinute;
        this.endTimeHour = endTimeHour;
        this.endTimeMinute = endTimeMinute;
        this.alarmTimeType = alarmTimeType;
        this.alarmColor = alarmColor;
        this.local = local;
        this.description = description;
        this.type = type;
        this.isComplete = isComplete;
    }


    public Schedule(int type) {
        this.type = type;
    }

    public String getYearMonthDay(){
        return this.year+"年"+this.month+"月"+this.day+"日";
    }

    public Calendar getRealAlarmTime() {
        if(type==0) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(getYear(), getMonth()-1, getDay());
            calendar.set(Calendar.HOUR_OF_DAY, getStartTimeHour());
            calendar.set(Calendar.MINUTE, getStartTimeMinute());
            calendar.set(Calendar.SECOND, 0);
            switch (getAlarmTimeType()) {
                case 0:
                    break;
                case 1:
                    calendar.add(Calendar.MINUTE, -10);
                    break;
                case 2:
                    calendar.add(Calendar.HOUR_OF_DAY, -1);
                    break;
                case 3:
                    calendar.add(Calendar.DAY_OF_MONTH, -1);
                    break;
            }
            return calendar;
        }
        else {
            Calendar calendar = Calendar.getInstance();
            calendar.set(getYear(), getMonth()-1, getDay());
            calendar.set(Calendar.HOUR_OF_DAY, getEndTimeHour());
            calendar.set(Calendar.MINUTE, getEndTimeMinute());
            calendar.set(Calendar.SECOND, 0);
            switch (getAlarmTimeType()) {
                case 0:
                    break;
                case 1:
                    calendar.add(Calendar.MINUTE, -10);
                    break;
                case 2:
                    calendar.add(Calendar.HOUR_OF_DAY, -1);
                    break;
                case 3:
                    calendar.add(Calendar.DAY_OF_MONTH, -1);
                    break;
            }
            return calendar;
        }

    }
    public void schedule(Context context) {
        Intent myIntent = new Intent("AlarmAlertBroadcastreceiver");


        Log.i("wqs","通知设置了3");
        Log.i("wqs",toString());
        myIntent.putExtra("scheduleTitle", title);
        myIntent.putExtra("alarmType", alarmTimeType);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, myIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, getRealAlarmTime().getTimeInMillis(), pendingIntent);
        //设置带有重复的提醒
//        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, getRealAlarmTime().getTimeInMillis(),getRaplayTime(),pendingIntent);
    }

    public Calendar getCurrentTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        return calendar;
    }

}
